package com.zuul.cloudzull.zuul;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;

import javax.servlet.http.HttpServletRequest;

//@Component
public class TokenFilter extends ZuulFilter {
    @Override
    public String filterType() {
        //前置过滤
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        //请求和响应
        // 获取请求上下文对象 ==》 ThreadLocal (本地线程变量)
        RequestContext ctx= RequestContext.getCurrentContext();
        //获取请求对象
        HttpServletRequest request=ctx.getRequest();
        //获取传来的参数accessToken
        Object accessToken=request.getParameter("accessToken");
        /**
         *  令牌需要重新计算和验证
         */
        if(accessToken==null){
            //过滤该请求，
            ctx.setSendZuulResponse(false);
            ctx.setResponseStatusCode(401);
            ctx.setResponseBody("{\"result:\":\"accessToken is empty!\"}");
        }
        return null;
    }
}
